import 'dart:io';

import 'package:clean_app/asset/color.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class SelectPic extends StatefulWidget {
  List<String> levels;
  SelectPic({super.key, required this.levels});

  @override
  State<SelectPic> createState() => _SelectPicState();
}

class _SelectPicState extends State<SelectPic> {
  final ImagePicker _picker = ImagePicker();

  @override
  void initState() {
    super.initState();
  }

  Future<void> _selectImage() async {
    final XFile? image = await _picker.pickImage(
      source: ImageSource.gallery,
    );

    if (image != null) {
      if (context.mounted) {
        Navigator.of(context).pushNamed("/process_img", arguments: {
          "file": image,
          "levels": widget.levels,
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          icon: Icon(Icons.chevron_left_rounded),
        ),
        backgroundColor: os_white,
      ),
      backgroundColor: os_white,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Text(
              widget.levels.join(" → "),
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.normal,
                color: os_light_light_dark_card,
              ),
            ),
          ),
          Container(height: 80),
          Container(
            width: 200,
            height: 200,
            decoration: BoxDecoration(
              color: os_color.withOpacity(0.1),
              borderRadius: BorderRadius.circular(1000),
              border: Border.all(
                color: os_color,
                width: 2,
              ),
            ),
            child: IconButton(
              onPressed: _selectImage,
              icon: Icon(
                Icons.add_photo_alternate,
                size: 80,
                color: os_color,
              ),
            ),
          ),
          Container(height: 30),
          // Text(
          //   "从相册选择图片",
          //   style: TextStyle(
          //     fontSize: 18,
          //     fontWeight: FontWeight.bold,
          //     color: os_color,
          //   ),
          // ),
          Container(height: 50),
          Container(
            width: MediaQuery.of(context).size.width * 0.8,
            child: ElevatedButton(
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(os_color),
                foregroundColor: WidgetStateProperty.all(os_white),
                padding: WidgetStateProperty.all(
                  EdgeInsets.only(top: 15, bottom: 15),
                ),
                shape: WidgetStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(100),
                  ),
                ),
              ),
              onPressed: _selectImage,
              child: Text(
                "选择图片",
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          Container(height: 100),
        ],
      ),
    );
  }
}
